<?php
include_once('common/header.php');
if($AppUI->id <= 0 || (isset($_SESSION['education']) && (($_SESSION['education']&1<<1) == 0))) {
	$this->_redirect(HOST_BACKEND);
}
$intCategoryId		= (int)$objRequest->getParam('pcategoryid',0);
$intSubId			= (int)$objRequest->getParam('psubid',0);
$strName			= trim($objRequest->getParam('pname',''));
$dblLng				= (double)$objRequest->getParam('plongitude',0.0);
$dblLat				= (double)$objRequest->getParam('platitude',0.0);
$strCountry			= $objRequest->getParam('pcountry',COUNTRY_DEFAULT);
$intProvince		= (int)$objRequest->getParam('pprovince',0);
$intDistrict		= (int)$objRequest->getParam('pdistrict',0);
$strAddr			= trim($objRequest->getParam('paddr',''));
$strFullAddr		= trim($objRequest->getParam('pfulladdr',''));
$strShort			= $objRequest->getParam('pshort','');
$strContent			= $objRequest->getParam('pcontent','');
$oFile				= $_FILES['pimage'];
$strLink			= $objRequest->getParam('plink','');
$intParentId		= $objRequest->getParam('pparentid',0);
$strTel				= $objRequest->getParam('ptel','');
$strFax				= $objRequest->getParam('pfax','');
$strEmail			= $objRequest->getParam('pemail','');
$strTag				= $objRequest->getParam('ptag','');
$intAlbumId			= $objRequest->getParam('palbumid',0);
$intType			= $objRequest->getParam('ptype',EDUCATION_TYPE);
$arrSpec			= $objRequest->getParam('pspec',null);
$strFeature			= $objRequest->getParam('ptbfeature','');
$arrErr				= null;
$strImage			= '';
// data allow
$strName			= Ecore_Function::stripTags($strName,-2);
$strShort			= Ecore_Function::stripTags($strShort,-2);
$strContent			= Ecore_Function::stripTags($strContent,-1);
$strAddr			= Ecore_Function::stripTags($strAddr,-2);
$strLink			= Ecore_Function::stripTags($strLink,-2);
$strTel				= Ecore_Function::stripTags($strTel,-2);
$strFax				= Ecore_Function::stripTags($strFax,-2);
$strEmail			= Ecore_Function::stripTags($strEmail,-2);
$strTag				= Ecore_Function::stripTags($strTag,-2);
$strFeature			= Ecore_Function::stripTags($strFeature,0);
//validate
if($intCategoryId < 1) array_push($arrErr, $arrLocale['categoryid_required']);
if($strName == '') array_push($arrErr, $arrLocale['name_required']);
elseif($strName != '' && strlen($strName) < VL_NAME_MINLENGTH) array_push($arrErr, $arrLocale['name_minlength']);
/*
if($dblLng == '') array_push($arrErr, $arrLocale['longitude_required']);
elseif($dblLng <= 0) array_push($arrErr, $arrLocale['longitude_number']);
if($dblLat == '') array_push($arrErr, $arrLocale['latitude_required']);
elseif($dblLat <= 0) array_push($arrErr, $arrLocale['latitude_number']);
*/
if($intProvince < 1) array_push($arrErr, $arrLocale['province_required']);
if($intDistrict < 1) array_push($arrErr, $arrLocale['district_required']);
if($strAddr == '') array_push($arrErr, $arrLocale['addr_required']);
if($strContent != '' && strlen($strContent) < VL_CONTENT_MINLENGTH) array_push($arrErr, $arrLocale['content_minlength']);
if($strLink != '' && !Ecore_Function::validateUrl($strLink)) array_push($arrErr, $arrLocale['link_format']);

if(!isset($arrErr) && isset($oFile)) {
	$arrImage	= Ecore_Upload_Education::upload($oFile);
	if($arrImage['error'] == 0) {
		$strImage	= $arrImage['path'];
	}else {
		array_push($arrErr, $arrLocale['upload']);
	}
}

if(!isset($arrErr)) {
	if(isset($_SESSION['EDUCATION_POST'])) unset($_SESSION['EDUCATION_POST']);
	if(isset($_SESSION['EDUCATION_ERROR'])) unset($_SESSION['EDUCATION_ERROR']);
	$intUserId			= ($AppUI->id)*-1;	
	
	if(isset($strFullAddr) && ($dblLng == '' || $dblLng == 0.0 || $dblLat == '' || $dblLat == 0.0)) {
		$arrLocation 	= Ecore_Function::getCoordinatesFromAddress($strFullAddr, $strCountry);		
		$dblLng			= $arrLocation->lng;
		$dblLat			= $arrLocation->lat;		
	} else {
		$dblLng			= str_replace(',','.',$dblLng);
		$dblLat			= str_replace(',','.',$dblLat);	
	}	
	$strShort			= $strShort != '' ? $strShort : Ecore_Function::wordCut($strContent); 			
	$objEducation 		= new Education();
	$intError			= $objEducation->add($AppUI->id, $intUserId, $strName, $intCategoryId, $intSubId,  
											 $strImage, $dblLat, $dblLng, $strAddr, $intDistrict, 
											 $intProvince, $strCountry, $strTel, $strFax, $strEmail, 
											 $strContent, $strShort, $strLink, $intParentId, $strTag,  
											 $strFeature, $intAlbumId, $intType, $strFullAddr, $arrSpec);
											 
	if($intError == 0) Ecore_Function::setMsg($arrMessage['education_add']);
	else array_push($arrErr, $arrError['noperm_education_add']);
}
if($arrErr) {
	$_SESSION['EDUCATION_POST']		= $objRequest->getParams();
	$_SESSION['EDUCATION_ERROR']	= $arrErr;
	$this->_redirect(HOST_BACKEND . 'education/add?parentid=' . $intParentId . '&prov=' . $intProvince . '&cate=' . $intCategoryId . '&dist=' . $intDistrict . '&type=' . $intType);
}
$this->_redirect(isset($_SESSION[SESSION_BACKURL]) ? $_SESSION[SESSION_BACKURL] : HOST_BACKEND . 'education/index');